package com.yx.learn.action;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.yx.learn.domain.Company;
import com.yx.learn.service.CompanyService;

@Controller("companyAction")
@Scope("prototype")
//采用modelDriven，简化表单数据接收
public class CompanyAction extends BaseAction implements ModelDriven<Company>{

	private static final long serialVersionUID = 1L;
	private Company company;
	@Resource
	private CompanyService companyService;
	private int page;
	private int rows;
	
	public void save(){
		try {
			company.setCreateTime(new Date());
			companyService.saveCompany(company);
			this.toResponse("{\"status\":\"ok\"}");
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	public void delete(){
		try {
			if(company.getId()!=1){
				companyService.deleteCompany(company);
			}
			this.toResponse("{\"status\":\"ok\"}");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void update(){
		try {
			if(company.getId()!=1){
				company.setCreateTime(new Date());
				companyService.updateCompany(company);
			}
			this.toResponse("{\"status\":\"ok\"}");
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	public void findAll(){
		try {
			String json = companyService.findAllToJson();
			this.toResponse(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public void findCompanies(){
		try {
			String json = companyService.findCompanies(page,rows);
			this.toResponse(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	
	public void companiesToCombobox(){
		try {
			String json = companyService.companiesToCombobox();
			this.toResponse(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}

	public Company getModel() {
		if(company==null){
			company =  new Company();
		}
		return company;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}	
}
